<template>
  <div>
    <!-- button按钮 -->
    <el-row>
      <el-col :span="12">
        <el-button type="primary" @click="drawer = true">新增信息</el-button>
        <el-button type="danger">批量删除</el-button>
      </el-col>
      <el-col :span="12">
        <div>
          <el-input placeholder="请输入内容" v-model="input" class="input-with-select">
            <el-button
              slot="append"
              @click="handleSearch"
              icon="el-icon-search"
            ></el-button>
          </el-input>
        </div>
      </el-col>
    </el-row>

    <!-- table表格 -->
    <el-table
      :data="list"
      border
      height="500px"
      style="width: 100%; margin-top: 20px"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column type="index" label="#" width="180"> </el-table-column>
      <el-table-column prop="title" label="标题" width="300"> </el-table-column>
      <el-table-column prop="author" label="作者" width="180"> </el-table-column>
      <el-table-column prop="time" label="用时" width="180"> </el-table-column>
      <el-table-column prop="cid" label="类别" width="180"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="150">
        <template slot-scope="scope">
          <el-button type="text" @click="handleUpdateClk(scope.row)" size="small"
            >修改</el-button
          >
          <el-button @click="handleClick(scope.row)" type="text" size="small"
            >查看</el-button
          >
          <el-button type="text" @click="handleDelClk(scope.row.id)" size="small"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- from表单 -->
    <el-drawer title="新增信息" :visible.sync="drawer" size="50%">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="问题">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="类别">
          <el-select v-model="form.cid" placeholder="请选择类别">
            <el-option
              v-for="(v, i) in classify"
              :label="v.name"
              :value="v.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时间">
          <el-select v-model="form.time" placeholder="请选择时间">
            <el-option label="一分钟" value="1"></el-option>
            <el-option label="二分钟" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="内容">
          <mavonEditor
            style="max-height: 300px; overflow-y: scroll"
            v-model="form.content"
          ></mavonEditor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">立即创建</el-button>
          <el-button @click="drawer = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { mavonEditor } from "mavon-editor";
import "mavon-editor/dist/css/index.css";

import { mapActions, mapState, mapMutations } from "vuex";
export default {
  data() {
    return {
      input: "",
      tableData: [],
      multipleSelection: [],
      drawer: false,
      form: {},
    };
  },
  mounted() {
    this.GetList();
  },
  methods: {
    ...mapActions("msbd", ["GetList", "DelList", "AddList", "UpdateList"]),
    ...mapMutations("msbd", ["SET_PAGESIZE", "SET_CURRENTPAGE", "SET_IPTVAL"]),
    handleSearch() {
      this.SET_IPTVAL(this.input);
      this.GetList();
    },
    handleSizeChange(val) {
      this.SET_PAGESIZE(val);
      this.GetList();
    },
    handleCurrentChange(val) {
      this.SET_CURRENTPAGE(val);
      this.GetList();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    async handleAdd() {
      if (this.form.id) {
        await this.UpdateList(this.form);
        await this.GetList();
      } else {
        await this.AddList(this.form);
        await this.GetList();
      }
      this.drawer = false;
    },
    handleDelClk(id) {
      this.DelList(id);
      this.GetList();
    },
    handleUpdateClk(item) {
      this.form = { ...item };
      this.drawer = true;
    },
  },
  computed: {
    ...mapState("msbd", ["list", "classify", "pageSize", "currentPage", "total"]),
  },
  components: {
    mavonEditor,
  },
};
</script>

<style scoped></style>
